package revontuli.aMHD.UIhelpers;

import java.util.ArrayList;

import revontuli.aMHD.dataclasses.Line;
import revontuli.aMHD.LineScheduleAct;
import revontuli.aMHD.aMHDApp;
import revontuli.aMHD.R;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class LineListAdapter extends BaseAdapter implements OnClickListener {
	private ArrayList<Line> mList = new ArrayList<Line>();
	private Context mContext;

	public LineListAdapter(Context context, ArrayList<Line> list) {
		this.mContext = context;
		this.mList = list;
	}
	
	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int pos) {
		return mList.get(pos);
	}

	@Override
	public long getItemId(int pos) {
		return pos;
	}

	@Override
	public View getView(int pos, View mView, ViewGroup mViewGroup) {
		Line mLine = mList.get(pos);
		
		if (mView == null) {
			LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			mView = mInflater.inflate(R.layout.linelistitem, null);
		}
		
		RelativeLayout riadok = (RelativeLayout) mView.findViewById(R.id.LineListItem);
		riadok.setOnClickListener(this);
		riadok.setTag(mLine.id);
		
		TextView mName = (TextView) mView.findViewById(R.id.LineListItemText);
		if (mLine.vehicle == aMHDApp.VEHICLE_BUS) {
			mName.setText(mContext.getResources().getString(R.string.bus)+" "+mLine.name);
		} else if (mLine.vehicle == aMHDApp.VEHICLE_TRAM) {
			mName.setText(mContext.getResources().getString(R.string.tram)+" "+mLine.name);
		} else if (mLine.vehicle == aMHDApp.VEHICLE_TROLLEY) {
			mName.setText(mContext.getResources().getString(R.string.trolley)+" "+mLine.name);
		} else if (mLine.vehicle == aMHDApp.VEHICLE_NIGHT) {
			mName.setText(mContext.getResources().getString(R.string.night)+" "+mLine.name);
		} else if (mLine.vehicle == aMHDApp.VEHICLE_REGIONAL) {
			mName.setText(mContext.getResources().getString(R.string.regional)+" "+mLine.name);
		}
		
		RatingBar mStar = (RatingBar) mView.findViewById(R.id.LineListItemStar);
		mStar.setOnTouchListener(mStarListener);
		mStar.setTag(mLine.id);
		if (mLine.update == true) {
			mStar.setRating(1);
		} else {
			mStar.setRating(0);
		}
		
		return mView;
	}
	
	private OnTouchListener mStarListener = new OnTouchListener() {
		
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			if (event.getAction() == MotionEvent.ACTION_UP) {
				RatingBar rb = (RatingBar) v;
				rb.setRating((rb.getRating()+1) % 2);
				aMHDApp.mDataManager.setLineFavourite((Integer)v.getTag(), (int)rb.getRating());
			}
			return true;
		}
	};

	@Override
	public void onClick(View v) {
		Intent in = new Intent(mContext, LineScheduleAct.class);
		in.putExtra("lineID", (Integer)v.getTag(0));
		in.putExtra("stopID", (Integer)null);
		mContext.startActivity(in);
	}
	

}
